package javase2.day03;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;

/**
 * 
 * IO工具类
 *
 */
public class IOUtils {
	public static void cp(String src,String dst){
		try{
			InputStream in = new FileInputStream(src);
			OutputStream out = new FileOutputStream(dst);
			int count;
			byte[] buf = new byte[1024*512];
			while((count=in.read(buf))!=-1){
				out.write(buf,0,count);
			}
			in.close();
			out.close();
		}catch(IOException e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	/**
	 * 复制文件，不支持文件夹的复制
	 * @param src 源文件名
	 * @param dst 目标文件名
	 */
	public static void cp1(String src,String dst){
		try{
			//打开源文件
			InputStream in = new FileInputStream(src);
			//打开目标文件
			OutputStream out = new FileOutputStream(dst);
			int b;
			while((b=in.read())!=-1){
				out.write(b);
			}
			in.close();
			out.close();
		}catch(IOException e){
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
	/**
	 * 
	 * @实现文件在控制台上打印输出
	 * 按照16进制形式打印，每16个byte为一行
	 * 
	 * file文件名FileNotFound
	 */
	public static void print(String file){
		try {
			RandomAccessFile raf = new RandomAccessFile(file,"r");
			int b;int i=1;
			while((b=raf.read())!=-1){
				if(b<=0xf){
					System.out.print("0");
				}
				System.out.print(Integer.toHexString(b)+" ");
				if(i++%16==0){
					System.out.println();
				}
			}
			System.out.println();
			raf.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
}
